package com.yto.hrreport.event.buttons {
	import com.adobe.serialization.json.JSON;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.containers.Panel;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.controls.Tree;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class SysAddButtonEvent extends ButtonEvent {
		
		private static var addEvent : SysAddButtonEvent = new SysAddButtonEvent();
		private static var parentName : String;
		
		private var txtNodeCode : TextInput;
		private var txtNodeId : TextInput;
		private var txtNodeName : TextInput;
		private var txtParentNodeCode : TextInput;
		private var chkIsParent : CheckBox;
		private var item : Object;
		private var headCanvas : Canvas;
		
		public function SysAddButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : SysAddButtonEvent {
			SysAddButtonEvent.parentName = parentName;
			return addEvent;
		}
		
		public function addEvent() : void {
			
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			headCanvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
			var bodyCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_bodyCanvas"));
			var hdivBox : HDividedBox = HDividedBox(bodyCanvas.getChildByName(parentName + "_hdivBox"));
			var sysTree : Tree = Tree(hdivBox.getChildByName(parentName + "_sysTree"));
			var sysPanel : Panel = Panel(hdivBox.getChildByName(parentName + "_sysFunc"));
			
			if(sysPanel == null) {
//				sysPanel  = Panel(hdivBox.getChildByName(parentName + "_sysTemplate"));
			} else {
				var sysCanvas : Canvas = Canvas(sysPanel.getElementAt(0));
				
				txtNodeCode = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeCode"));
				txtNodeId = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeId"));
				txtNodeName = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeName"));
				txtParentNodeCode = TextInput(sysCanvas.getChildByName(parentName + "_txtParentNodeCode"));
				chkIsParent = CheckBox(sysCanvas.getChildByName(parentName + "_chkIsParent"));
				
				if(sysTree.selectedItem != null) {
					item = sysTree.selectedItem;
					
					item.state = "add";
					
					sysTree.enabled = false;
					
					txtNodeCode.enabled = false;
					txtNodeId.enabled = true;
					txtNodeName.enabled = true;
					chkIsParent.enabled = true;
					
					executeNextNode(item);
					
				}
				
				
			}
			
		}
		
		private function executeNextNode(item : Object) : void {
			
			var nodeServer : RemoteObject = new RemoteObject;
			nodeServer.destination = IProcessor.NodeProcessor;
			nodeServer.resultNodeJSON(item.nodecode);
			nodeServer.addEventListener(ResultEvent.RESULT , resultHandler);
			
		}
		
		private function resultHandler(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult == null)
				lastResult = "[]";
			
			var result : Array = JSON.decode(lastResult) as Array;
			var nextNode : String;
			
			if(result.length == 0 || lastResult == "[]") {
				nextNode = "01";
			} else {
				var max : int = 0;
				
				for each(var child : Object in result) {
					var next : int = int(String(child.nodecode).substr(String(child.nodecode).length - 2 , String(child.nodecode).length));
					if(next > max)
						max = next;
				}
				
				max ++;
				
				if(max < 10)
					nextNode = "0" + String(max);
				
				nextNode = String(max);
			}
			
			txtParentNodeCode.text = item.nodecode;
			txtNodeCode.text = item.nodecode + nextNode;
			txtNodeId.text = null;
			txtNodeName.text = null;
			chkIsParent.selected = false;
			
			updateButtons(null , "sysEdit" , headCanvas , true); 
			
		}
		
	}
}